﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Infra_Data.Mapping;

namespace xiaodingmarket_Infra_Data.Context
{
    public class MarketContext : DbContext
    {
        public MarketContext(DbContextOptions<MarketContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //属性配置
            //modelBuilder.Entity<Commodity>().Property(p => p.endtime).HasColumnType("DATETIME2");
            //modelBuilder.Entity<Commodity>().HasIndex(p=>p.Category);//单列索引

            modelBuilder.ApplyConfiguration(new CommodityMap());
            modelBuilder.ApplyConfiguration(new OrderMap());
            modelBuilder.ApplyConfiguration(new ReportMap());
            //modelBuilder.ComplexType<Address>();找不到程序集引用，用特性来标注复杂类型
            //多对多关系（也可以不写自动配置）
            modelBuilder.Entity<OrderDetail>().Property(p => p.price).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<ModuleRoles>()
                .HasOne(x => x.Module).WithMany(x => x.ModuleRoles).HasForeignKey(x => x.ModuleID);
            modelBuilder.Entity<ModuleRoles>()
                .HasOne(x => x.Role).WithMany(x => x.moduleoles).HasForeignKey(x => x.RoleID);
            modelBuilder.Entity<UserRoles>()
                .HasOne(x => x.Role).WithMany(x => x.userroles).HasForeignKey(x => x.RoleID);
            modelBuilder.Entity<UserRoles>()
                .HasOne(x => x.User).WithMany(x => x.userroles).HasForeignKey(x => x.UserID);

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<User> user { get; set; }
        public DbSet<Role> role { get; set; }
       // public DbSet<Address> adrress { get; set; }
        public DbSet<Commodity> commodities { get; set; }
        public DbSet<CommodityDetail> commodityDetails { get; set; }
        public DbSet<Module> module { get; set; }
        public DbSet<ModuleRoles> moduleRoles { get; set; }
        public DbSet<OrderCommodity> orderCommodities { get; set; }
        public DbSet<OrderDetail> orderDetails { get; set; }
        public DbSet<Report> reports { get; set; }
        public DbSet<Reportprocess> reportprocesses { get; set; }
        public DbSet<UserRoles> UserRoles { get; set; }

    }
}
